﻿using ImageK.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace ImageK
{
    public class RecentOpener
    {
        private string path;

        public RecentOpener(string path)
        {
            this.path = path;
            //此处修改ImageJ的方式，不在线程中启动新窗体，否则，窗体随着线程调用结束一闪就消失了
            // Thread thread = new Thread(run)
            // {
            //     Name = "RecentOpener"
            // };
            // thread.Start();
            run();
        }

        /** Open the file and move the path to top of the submenu. */
        public void run()
        {
            Opener o = new Opener();
            o.open(path);
            ToolStripMenuItem menu = Menus.getOpenRecentMenu();
            int n = menu.DropDownItems.Count;
            int index = 0;
            for (int i = 0; i<n; i++)
            {
                if (menu.DropDownItems[i].Text.Equals(path))
                {
                    index = i;
                    break;
                }
            }
            if (index>0)
            {
                var item = menu.DropDownItems[index];
                menu.DropDownItems.RemoveAt(index);
                menu.DropDownItems.Insert(0, item);
            }
        }
    }
}
